//
//  ViewController.m
//  IOSApplication
//
//  Created by 麻再挺 on 2019/5/20.
//  Copyright © 2019 麻再挺. All rights reserved.
//

#import "ViewController.h"
#import <UIImageView+WebCache.h>
#import "controller/DrawControllerViewController.h"

@interface ViewController ()
// 倒计时时间
#define TIME 1
// 倒计时标签, 赋值之后会调用新值的retain方法和旧值的release方法
@property(nonatomic, retain) UILabel *label;
// 倒计时时间, set 方法简单赋值
@property(nonatomic, assign) int time;
// 定时器,
@property(nonatomic, assign) NSTimer *timer;

@end

@implementation ViewController

// 视图加载完成
- (void)viewDidLoad {
    [super viewDidLoad];

    // 倒计时时间初始化
    _time = TIME;
    // Do any additional setup after loading the view, typically from a nib.
    // 添加一个紫色的视图
//    UIView *purpleView = [[UIView alloc] init];
//    [purpleView setBackgroundColor:UIColor.purpleColor];
//    [purpleView setFrame:CGRectMake(0, 100, 150, 150)];
//    [self.view addSubview:purpleView];
    // 获取根窗口
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    // 获取屏幕宽高
//    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
//    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    // 创建背景视图
    UIView *backgroundView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 添加图片
    [backgroundView addSubview:[self getBackgroundImage]];
    // 设置背景颜色
//    [backgroundView setBackgroundColor:UIColor.redColor];
    [self setTimerLabel];
    // 添加到背景中
    [backgroundView addSubview:_label];
    // 将背景视图添加到根布局
    [self.view addSubview:backgroundView];
    // 设置定时器
    [self setTimer];
}

// 获取背景图片
// @param width 宽度
// @param height 高度
// @param 背景图
- (UIImageView *) getBackgroundImage {
    // 创建图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    // 创建一个 URL
    NSURL *url = [NSURL URLWithString: @"http://image.baidu.com/search/down?tn=download&word=download&ie=utf8&fr=detail&url=http%3A%2F%2Fhuafans.dbankcloud.com%2Fpic%2F2018%2F08%2F29%2F4636599de46bf01261904f9661f75460__DC594464FD1D66C184C4037226956760.jpg%3Fmode%3Ddownload&thumburl=http%3A%2F%2Fimg5.imgtn.bdimg.com%2Fit%2Fu%3D2574862072%2C"];
    // 加载图片
    [imageView sd_setImageWithURL:url];
    return imageView;
}

// 设置定时器标签
// @param width 宽度
// @param height 高度
- (void) setTimerLabel {
    // 设置 Label 位置
    _label = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 120, SCREEN_HEIGHT - 100, 100, 50)];
    // 设置背景颜色及透明度
    [_label setBackgroundColor: [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1 ]];
    // 设置时间
    [_label setText: [[NSString alloc] initWithFormat:@"倒计时%ds", _time]];
    // 设置文字颜色
    [_label setTextColor:UIColor.whiteColor];
    // 设置文字位置
    [_label setTextAlignment: NSTextAlignmentCenter];
    // 设置与用户交互
    _label.userInteractionEnabled = YES;
    // 创建手势点击
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTouchUpInside:)];
    // 添加点击事件
    [_label addGestureRecognizer:recognizer];
}

// 定时器 Label 点击事件
- (void) labelTouchUpInside: (UITapGestureRecognizer *)recognizer {
    NSLog(@"定时器 Label 点击");
    [self jumpMainController];
}

// 设置定时器
- (void) setTimer {
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        self->_time--;
        NSLog(@"时间: %d", self->_time);
        if (self->_time > 0) {
            // 修改文字
            [self->_label setText: [[NSString alloc] initWithFormat:@"倒计时%ds", self->_time]];
        } else {
            [self jumpMainController];
        }
    }];
}

// 清除定时器
- (void) clearTimer {
    // 取消定时器
    [_timer invalidate];
    _timer = nil;
}

// 跳转到主控制器
- (void) jumpMainController {
    // 清除定时器
    [self clearTimer];
    // 模态进入主控制器
    [self presentViewController:[DrawControllerViewController new] animated:YES completion:nil];
}
@end
